package com.zjdiepu.www.biz.base;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.base.BaseAccountBusiUnitMapper;
import com.zjdiepu.www.ibiz.base.IBaseAccountBusiUnitService;
import com.zjdiepu.www.model.base.dto.BaseAccountBusiUnit;
import com.zjdiepu.www.model.base.ext.BaseAccountBusiUnitExt;
import com.zjdiepu.www.model.system.vo.BaseBusiUnitVo;

/**
 * Service层操作实现<br/>
 * @author zhangzhengjian<br/>
 * @time 2017年05月31日 09时36分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseAccountBusiUnitServiceImpl extends BaseService implements IBaseAccountBusiUnitService {
	public static final Logger logger = LoggerFactory.getLogger(BaseAccountBusiUnitServiceImpl.class);

	@Autowired
	private BaseAccountBusiUnitMapper baseAccountBusiUnitMapper;
	
	@Override
	public BaseAccountBusiUnit saveOrUpdate(
			BaseAccountBusiUnit baseAccountBusiUnit, AbstractSysUser currentUser) {
		if(null == baseAccountBusiUnit) {
			return null;
		}
		BaseAccountBusiUnit baseAccountBusiUnitParam = new BaseAccountBusiUnit();
		baseAccountBusiUnitParam.setDpStatus(DpStatus.NORMAL.getCode());
		baseAccountBusiUnitParam.setUserId(baseAccountBusiUnit.getUserId());
		baseAccountBusiUnitParam.setIdUnit(baseAccountBusiUnit.getIdUnit());
		BaseAccountBusiUnit baseAccountBusiUnitDb = baseAccountBusiUnitMapper.selectRefined(baseAccountBusiUnitParam);
		
		String userId = currentUser.getId();
		String creatorName = currentUser.getName();
		if(null == baseAccountBusiUnitDb) {
			baseAccountBusiUnit.initInsertData(userId, creatorName);
			baseAccountBusiUnitMapper.insert(baseAccountBusiUnit);
		} else {
			baseAccountBusiUnit.initUpdateData(userId, creatorName);
			baseAccountBusiUnitMapper.updateByPrimaryKeySelective(baseAccountBusiUnit);
		}
		
		return baseAccountBusiUnit;
	}
	
	@Override
	public void saveUpdateAndDelete(BaseAccountBusiUnit baseAccountBusiUnit, OperateType operType) {
		switch (operType) {
		case add:
			baseAccountBusiUnit.initInsertData(baseAccountBusiUnit.getModifier(), baseAccountBusiUnit.getModifierName());
			baseAccountBusiUnitMapper.insert(baseAccountBusiUnit);
			break;
		case edit:
			baseAccountBusiUnit.initUpdateData(baseAccountBusiUnit.getModifier(), baseAccountBusiUnit.getModifierName());
			// 影响的记录数
			int updateCount = baseAccountBusiUnitMapper.updateByPrimaryKeySelective(baseAccountBusiUnit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseAccountBusiUnit.initDeleteData(baseAccountBusiUnit.getModifier(), baseAccountBusiUnit.getModifierName());
			// 影响的记录数
			int deleteCount = baseAccountBusiUnitMapper.updateByPrimaryKeySelective(baseAccountBusiUnit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public void deleteByExt(BaseAccountBusiUnitExt baseAccountBusiUnitExt) {
		baseAccountBusiUnitMapper.deleteByExt(baseAccountBusiUnitExt);
	}
	
	@Override
	public List<BaseAccountBusiUnit> selectByExt(BaseAccountBusiUnitExt baseAccountBusiUnitExt) {
		return baseAccountBusiUnitMapper.selectByExt(baseAccountBusiUnitExt);
	}
	
	@Override
	public int countByExt(BaseAccountBusiUnitExt baseAccountBusiUnitExt){
		return baseAccountBusiUnitMapper.countByExt(baseAccountBusiUnitExt);
	}
	
	@Override
	public BaseAccountBusiUnit selectById(String id) {
		return baseAccountBusiUnitMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseAccountBusiUnitMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseAccountBusiUnit baseAccountBusiUnit) {
		return baseAccountBusiUnitMapper.countRefined(baseAccountBusiUnit);
	}

	@Override
	public BaseAccountBusiUnit selectRefined(BaseAccountBusiUnit baseAccountBusiUnit) {
		return baseAccountBusiUnitMapper.selectRefined(baseAccountBusiUnit);
	}

	@Override
	public boolean repeat(BaseAccountBusiUnit baseAccountBusiUnit) {
		int count = countRefined(baseAccountBusiUnit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseAccountBusiUnit.getId())) {
			return true;
		}
		BaseAccountBusiUnit dbItem = selectRefined(baseAccountBusiUnit);
		if(null == dbItem) {
			return false;
		}
		return !baseAccountBusiUnit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseAccountBusiUnitMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public void insertBatch(List<BaseAccountBusiUnit> list) {
		
		baseAccountBusiUnitMapper.insertBatch(list);
	}

	@Override
	public int countMulitList(BaseAccountBusiUnitExt baseAccountBusiUnitExt) {
		return baseAccountBusiUnitMapper.countMulitList(baseAccountBusiUnitExt);
	}

	@Override
	public List<BaseBusiUnitVo> selectMulitList(Boolean  scene) {
		return baseAccountBusiUnitMapper.selectMulitList(scene);
	}
}
